(function() {
  function buildParams(jsonData) {
    var sortedKey = Object.keys(jsonData).sort();

    var ticketStr = "";
    for (var i = 0; i < sortedKey.length; i++) {
      var key = sortedKey[i];
      ticketStr = ticketStr.length > 0 ? ticketStr + "&" : "";
      ticketStr = ticketStr + key + "=" + jsonData[key];
    }

    return ticketStr + "xu7t6j8vmso3lrbcwz0hind5p14qfyge";
  }

  function encryParams(params) {
    var ticket = MD5(buildParams(params));
    params["ticket"] = ticket;
    return params;
  }

  window.jumpTo = (url, id) => {
    const params = { id };
    // 通过服务端返回ip，方便测试
    const urlParams = window.location.search.substring(1);
    if (urlParams) {
      const arr = urlParams.split('=');
      if (arr[0] === 'ip') {
        params.ip = arr[1];
      }
    }
    const data = encryParams(params);
    axios
      .request({
        method: "POST",
        url: "http://webapp.develop.com/adserver/api/reportinfo",
        data,
      })
      .then(function(response) {
        var _response$data = response.data;
        console.log(_response$data);
      })
      .catch(function(error) {
        console.log(error);
      });

    window.location.href = url;
  };

  // 倒计时
  let time = 15; // 15秒倒计时
  const clock = document.getElementById("clock");
  const button = document.getElementById("button");
  const timer = setInterval(() => {
    time -= 1;
    if (time === 0) {
      timer && clearInterval(timer);
      button.innerText = "点击跳过";
      // TODO
    }
    clock.innerText = `${time}秒`;
  }, 1000);

  // 获取右上角按钮点击事件
  button.onclick = () => {
    timer && clearInterval(timer);
    console.log("button onclick ... ...");
    // TODO
  };
})();
